package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.sky.constant.MessageConstant.SETMEAL_ENABLE_FAILED;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 新增的套餐默认为停售状态
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.save(setmeal);
        // 主键返回
        Long setmealId = setmeal.getId();
        // 保存套餐-菜品记录
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setMealDishMapper.saveBatch(setmealDishes);


    }

    /**
     * 套餐条件分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p = setmealMapper.page(setmealPageQueryDTO);
        log.info("套餐分页查询结果：{}", p);
        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 根据id批量删除套餐
     *
     * @param ids
     */
    public void deleteBatchByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(String.valueOf(id));
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                // 在售状态，不可删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        // 删除对应的套餐
        setmealMapper.deleteBatchByIds(ids);

        // 删除套餐对应的套餐菜品关系表数据
        setMealDishMapper.deleteBysetmealIds(ids);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(String id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        // 查询套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setMealDishMapper.getInfoBySetMealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 更新套餐表
        setmealMapper.update(setmeal);

        // 更新菜品套餐关系表
        // 删除对应的菜品套餐关系数据
        Long setmealId = setmealDTO.getId();
        List<Long> ids = new ArrayList<>();
        ids.add(setmealId);
        setMealDishMapper.deleteBysetmealIds(ids);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setMealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    public void startOrStopSetMeal(String status, String id) {
        // 套餐内如果有停售菜品，则套餐无法上架
        List<Dish> list = setMealDishMapper.getDishBySetmealId(id);
        for (Dish dish : list) {
            if (dish.getStatus() == StatusConstant.DISABLE) {
                // 套餐无法上架
                throw new SetmealEnableFailedException(SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.startOrStopSetMeal(status, id);
    }
}
